package cn.edu.ctbu.lablist.webapi;

import cn.edu.ctbu.lablist.core.PageUtils;
import cn.edu.ctbu.lablist.domain.Score;
import cn.edu.ctbu.lablist.domain.Student;
import cn.edu.ctbu.lablist.service.ScoreService;
import cn.edu.ctbu.lablist.service.TeacherService;
import cn.edu.ctbu.lablist.utils.RUtil;
import cn.edu.ctbu.lablist.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/webapi/score")
public class ScoreRestController {
    @Autowired
    private ScoreService scoreService;
    @GetMapping("/gettno")//根据教师编号查询所有选了这门课程的课程成绩信息
    public R<Score> get(String tno){
        List<Score> scores = scoreService.findByTno(tno);
        return RUtil.success(scores);
    }
    @GetMapping("/getsno/{sno}")//根据学号查询该学生所选的所有课程信息
    public R<Score> getsno(@PathVariable String sno){
        List<Score> scores = scoreService.findBySno(sno);
        return RUtil.success(scores);
    }
    @GetMapping("/get/{id}")//根据编号查询对应课程成绩表中的信息
    public R getid(@PathVariable Integer id){
        Score score = scoreService.findById(id);
        return RUtil.success(score);
    }
    //分页查询，当sno和tno都为空时查询所有信息，当tno不为空时查询该教师所教的所有学生，当sno不为空时查询该学生所选的所有课程信息
    @GetMapping("/getbypage")
    public R getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "tno",defaultValue = "") String tno,
                               @RequestParam(value = "sno",defaultValue = "") String sno){
        //按id排倒序
        Sort sort=Sort.by(Sort.Direction.DESC,"grade");
        Page<Score> scorePage = null;
        if(StringUtils.isEmpty(tno)&&StringUtils.isEmpty(sno)){
            Pageable pageables = PageRequest.of(page,size,sort);
            scorePage = scoreService.findAll(pageables);
        }
        else if((StringUtils.isEmpty(tno) == false)&&StringUtils.isEmpty(sno)){
            Score score = new Score();
            score.setTno(tno);
            Pageable pageables = PageRequest.of(page,size,sort);
            ExampleMatcher matchers = ExampleMatcher.matching();
            matchers = matchers.withMatcher("tno",ExampleMatcher.GenericPropertyMatchers.endsWith());
            Example<Score> examples = Example.of(score,matchers);
            scorePage = scoreService.findAll(examples,pageables);
        }
        else if((StringUtils.isEmpty(sno) == false)&&StringUtils.isEmpty(tno)){
            Score score = new Score();
            score.setSno(sno);
            Pageable pageables = PageRequest.of(page,size,sort);
            ExampleMatcher matcher = ExampleMatcher.matching();
            matcher = matcher.withMatcher("sno",ExampleMatcher.GenericPropertyMatchers.endsWith());
            Example<Score> examples = Example.of(score,matcher);
            scorePage = scoreService.findAll(examples,pageables);
        }
        PageUtils pageUtils = new PageUtils(scorePage.getContent(),scorePage.getTotalElements());
        return RUtil.success(pageUtils);
    }
    @PutMapping("/update")//修改选课信息
    public R<Score> update(Score score){
        //读取旧数据
        Score oldscore=scoreService.findById(Integer.valueOf(score.getId()));
        score.setSno(oldscore.getSno());
        score.setTno(oldscore.getTno());
        scoreService.update(score);
        return RUtil.success(score);
    }
    @PostMapping("/insert")//插入选课信息
    public R insert(@Valid Score score, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String errorMsg = bindingResult.getFieldError().getDefaultMessage();
            log.info("新增学生出错了!{}",errorMsg);
            return RUtil.error(-10,errorMsg);
        }
        scoreService.insert(score);
        Long myid = Long.valueOf(score.getId());

        return RUtil.success(myid.intValue());
    }
    @DeleteMapping("/delete/{id}")//删除选课信息
    public void delete(@PathVariable Integer id){
        scoreService.delete(id);
    }}